package com.ctg.behavior.calc.impala.transfer.usergroups;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.builder.usergroups.CtUGCalcReportBuilder;
import com.ctg.behavior.calc.impala.report.uesrgroups.CtCalcUGUSRuleReport;
import com.ctg.behavior.calc.impala.report.uesrgroups.CtUGUSSequenceReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcUserSequence;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUGUSRuleTransfer extends CtFilterTransfer {

	@Autowired
	private CtUGUSSequenceTransfer sequenceTransfer;
	@Autowired
	private CtUGCalcReportBuilder ugReportBuilder;

	/******************************************************
	 * @apiNote:
	 */
	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtCalcUGUSRuleReport)) {
			log.warn("calcObject is not CtUGUSRuleReport, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}

		CtCalcUGUSRuleReport usRule = (CtCalcUGUSRuleReport) calcObject;
		log.info("CalcBody={}", JSON.toJSONString(usRule));

		String ruleRelation = usRule.getRelation();
		ruleRelation = ruleRelation.toUpperCase();

		// 此层SQL的表查询别名
		String thisAlias = "ugus";

		StringBuilder impalaSQL = new StringBuilder();
		impalaSQL.append(" SELECT DISTINCT ").append(thisAlias).append(".user_id AS userId ");
		impalaSQL.append(" FROM a_event_parquet_").append(usRule.getAppId());
		// 增加别名
		impalaSQL.append(" ").append(thisAlias).append(" ");
		impalaSQL.append(" WHERE ");

		List<CtCalcUserSequence> sequences = usRule.getSequences();

		if (sequences.isEmpty()) {
			// 若用户的行为序列规则没有指定，则不增加此SQL条件
			return "";
		}

		for (int sequenceIndex = 0; sequenceIndex < sequences.size(); sequenceIndex++) {
			CtCalcUserSequence oneSequence = sequences.get(sequenceIndex);
			// 把sequences数组中的一个CtCalcUserSequence对象转换为SQL条件
			CtUGUSSequenceReport sequenceReprot = null;
			sequenceReprot = ugReportBuilder.buildUGUSSequenceReport(oneSequence, usRule.getAppId());
			String sequenceSQL = sequenceTransfer.doTransfer(sequenceReprot, sqlParam);
			// impalaSQL.append(" device_id IN ( ").append(sequenceSQL).append(" ) ");
			// 增加别名
			impalaSQL.append(" ").append(thisAlias).append(".user_id IN ( ");
			impalaSQL.append(" SELECT userId FROM ( ").append(sequenceSQL).append(" ) ustm").append(sequenceIndex).append(" ");
			impalaSQL.append(" ) ");
			if (sequenceIndex < sequences.size() - 1) {
				impalaSQL.append(" ").append(ruleRelation).append(" ");
			}
		}

//		// GROUP BY devcie_id
//		impalaSQL.append(" GROUP BY ").append(thisAlias).append(".device_id ");

		String retSQL = impalaSQL.toString();
		log.info("CalcSQL={}", retSQL);

		return retSQL;
	}

}
